package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private RoomAttrValueService roomAttrValueService;
    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private AttrValueMapper attrValueMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Override
    public void customSaveOrUpdate(RoomSubmitVo roomSubmitVo) {

        Boolean isUpdate = roomSubmitVo.getId() != null;
        saveOrUpdate(roomSubmitVo);
        Long rootId = roomSubmitVo.getId();
        if (isUpdate) {
            // 图片
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, rootId);
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
            graphInfoService.remove(graphInfoLambdaQueryWrapper);
            // 房间属性
            LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId, rootId);
            roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);
            // 房间配套
            LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomFacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId, rootId);
            roomFacilityService.remove(roomFacilityLambdaQueryWrapper);
            // 房间标签
            LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId, rootId);
            roomLabelService.remove(roomLabelLambdaQueryWrapper);
            // 房间支付方式
            LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId, rootId);
            roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);
            // 房间租期
            LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId, rootId);
            roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);
        }
        // 保存
        // 图片
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        ArrayList<GraphInfo> graphInfos = new ArrayList<>(graphVoList.size());
        for (GraphVo graphVo : graphVoList) {
            GraphInfo graphInfo = GraphInfo.builder()
                    .name(graphVo.getName())
                    .url(graphVo.getUrl())
                    .itemType(ItemType.ROOM)
                    .itemId(rootId)
                    .build();
            graphInfos.add(graphInfo);
        }
        graphInfoService.saveBatch(graphInfos);
        // 房间属性
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        if(!CollectionUtils.isEmpty(attrValueIds)){
        ArrayList<RoomAttrValue> roomAttrValues = new ArrayList<>();
        for (Long attrValueId : attrValueIds) {
            RoomAttrValue roomAttrValue = RoomAttrValue.builder()
                    .roomId(rootId)
                    .attrValueId(attrValueId)
                    .build();
            roomAttrValues.add(roomAttrValue);
        }
        roomAttrValueService.saveBatch(roomAttrValues);
        }
        // 房间配套
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        if(!CollectionUtils.isEmpty(facilityInfoIds)){
        ArrayList<RoomFacility> roomFacilities = new ArrayList<>();
        for (Long facilityInfoId : facilityInfoIds) {
            RoomFacility roomFacility = RoomFacility.builder()
                    .roomId(rootId)
                    .facilityId(facilityInfoId)
                    .build();
            roomFacilities.add(roomFacility);
        }
        roomFacilityService.saveBatch(roomFacilities);

        }
        // 房间标签
        if(!CollectionUtils.isEmpty(attrValueIds)){

        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        ArrayList<RoomLabel> roomLabels = new ArrayList<>();
        for (Long labelInfoId : labelInfoIds) {
            RoomLabel roomLabel = RoomLabel.builder()
                    .roomId(rootId)
                    .labelId(labelInfoId)
                    .build();
            roomLabels.add(roomLabel);
        }
        roomLabelService.saveBatch(roomLabels);
        }
        // 房间支付方式
        if(!CollectionUtils.isEmpty(attrValueIds)){
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        ArrayList<RoomPaymentType> roomPaymentTypes = new ArrayList<>();
        for (Long paymentTypeId : paymentTypeIds) {
            RoomPaymentType paymentType = RoomPaymentType.builder()
                    .paymentTypeId(paymentTypeId)
                    .roomId(rootId)
                    .build();
            roomPaymentTypes.add(paymentType);
        }
        roomPaymentTypeService.saveBatch(roomPaymentTypes);

        }
        // 房间租期
        if(!CollectionUtils.isEmpty(attrValueIds)){
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        ArrayList<RoomLeaseTerm> roomLeaseTerms = new ArrayList<>();
        for (Long leaseTermId : leaseTermIds) {
            RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder()
                    .leaseTermId(leaseTermId)
                    .roomId(rootId)
                    .build();
            roomLeaseTerms.add(roomLeaseTerm);
        }
        roomLeaseTermService.saveBatch(roomLeaseTerms);
        }
    }

    @Override
    public IPage<RoomItemVo> customQueryList(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        page = roomInfoMapper.customQueryList(page,queryVo);
        return page;
    }

    @Override
    public RoomDetailVo customQueryListByRoomId(Long id) {
        // 房间
        RoomInfo roomInfo = getById(id);
        //  公寓
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());
        // 图片
        List<GraphVo> graphVos = graphInfoMapper.customQueryListById(ItemType.ROOM, id);
        List<AttrValueVo> attrValueVos = attrValueMapper.customQueryListByRoomId(id);
        List<FacilityInfo> facilityInfos = facilityInfoMapper.customQueryListByRoomId(id);
        List<LabelInfo> labelInfos = labelInfoMapper.customQueryListByRoomId(id);
        List<PaymentType> paymentTypes = paymentTypeMapper.customQueryListByRoomById(id);
        List<LeaseTerm> leaseTerms = leaseTermMapper.customQueryListByRoomById(id);
        RoomDetailVo roomDetailVo = RoomDetailVo.builder()
                .apartmentInfo(apartmentInfo)
                .graphVoList(graphVos)
                .attrValueVoList(attrValueVos)
                .facilityInfoList(facilityInfos)
                .labelInfoList(labelInfos)
                .paymentTypeList(paymentTypes)
                .leaseTermList(leaseTerms)
                .build();

        BeanUtils.copyProperties(roomInfo, roomDetailVo);
        return roomDetailVo;
    }

    @Override
    public void removeByRoomId(Long id) {
        Boolean isCheckIn =  roomInfoMapper.getIsCheckIn(id);
        if (isCheckIn){
            new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }
        // 删除房间
        removeById(id);
        // 删除图片
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getId, id);
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
        graphInfoService.remove(graphInfoLambdaQueryWrapper);
        // 删除房间属性
        LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomFacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId, id);
        roomFacilityService.remove(roomFacilityLambdaQueryWrapper);
        // 删除标签
        LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId, id);
        roomLabelService.remove(roomLabelLambdaQueryWrapper);
        // 删除 支付方式
        LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId, id);
        roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);
        // 删除房间租期
        LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId, id);
        roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);
    }


}




